﻿Public Class GestClient
    Dim position As Integer = 0

#Region "Evenements"

    Private Sub GestClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = FrmStart 'Modifier Param IsContainer à true

        MySQLConnect.MaRequete = "SELECT * FROM Client"
        MySQLConnect.MaCommandeSpecialeRequete.CommandText = MySQLConnect.MaRequete
        MySQLConnect.MonRemplisseur.SelectCommand = MySQLConnect.MaCommandeSpecialeRequete
        MySQLConnect.MonRemplisseur.Fill(MySQLConnect.MonJeuDeDonnees, "TousLesClients")
        DGClients.DataSource = MySQLConnect.MonJeuDeDonnees.Tables("TousLesClients")

        txtNum.Enabled = False

        Rafraichir()
    End Sub

    Private Sub GestClient_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        'Fermeture de la fenêtre
        MySQLConnect.MonJeuDeDonnees.Tables("TousLesClients").Clear() 'On vide le jeu de données
        Me.Hide()
    End Sub

    Private Sub DGClients_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DGClients.Click
        If DGClients.CurrentCell.Value Is DBNull.Value Then
            txtNum.Visible = False
            txtNom.Visible = False
            txtPrenom.Visible = False
            txtRue.Visible = False
            txtCodePostal.Visible = False
            txtVille.Visible = False
        Else
            txtNum.Visible = True
            txtNom.Visible = True
            txtPrenom.Visible = True
            txtRue.Visible = True
            txtCodePostal.Visible = True
            txtVille.Visible = True

            txtNum.Text = DGClients.CurrentRow.Cells(0).Value()
            txtNom.Text = DGClients.CurrentRow.Cells(1).Value()
            txtPrenom.Text = DGClients.CurrentRow.Cells(2).Value()
            txtRue.Text = DGClients.CurrentRow.Cells(3).Value()
            txtCodePostal.Text = DGClients.CurrentRow.Cells(4).Value()
            txtVille.Text = DGClients.CurrentRow.Cells(5).Value()
        End If
    End Sub

#End Region

#Region "Gestion des Boutons"
    Private Sub btnAjouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAjouter.Click
        'Gestion de l'ajout de client
        If btnAjouter.Text = "Ajouter" Then
            txtNum.Text = MySQLConnect.GenererClePrimaire("idClient", "Client") '(MySQLConnect.MonJeuDeDonnees.Tables("TousLesClients").Rows.Count) + 1 'Incrémentation de 1 
            txtNom.Text = ""
            txtPrenom.Text = ""
            txtRue.Text = ""
            txtCodePostal.Text = ""
            txtVille.Text = ""

            btnAjouter.Text = "Valider !"
        Else
            If txtNom.Text <> "" And txtPrenom.Text <> "" And txtVille.Text <> "" Then
                Dim num As Integer
                Dim nom As String
                Dim prenom As String
                Dim rue As String
                Dim code As String
                Dim ville As String

                'Affectation variables
                num = CInt(txtNum.Text)
                nom = txtNom.Text
                prenom = txtPrenom.Text
                rue = txtRue.Text
                code = CInt(txtCodePostal.Text)
                ville = txtVille.Text

                'Requête
                MySQLConnect.MaRequete = "INSERT INTO Client VALUES (" & num & ", '" & nom & "', '" & prenom & "', '" & rue & "', " & code & ", '" & ville & "'  )"
                MySQLConnect.MaCommandeSpecialeRequete.CommandText = MySQLConnect.MaRequete
                MySQLConnect.MaCommandeSpecialeRequete.ExecuteNonQuery()

                MessageBox.Show("Le client a été ajouté !", "Ajout d'un client", MessageBoxButtons.OK)

                position = 0
                RechargerLesDonnees()
                Rafraichir()

                btnAjouter.Text = "Ajouter"
            Else
                MessageBox.Show("Veuillez renseignez tous les champs munis d'un '*'", "Erreur !", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        End If
    End Sub

    Private Sub btnSuppr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSuppr.Click
        Dim result = MessageBox.Show("Etes-vous sur de supprimer ce client ?", "Suppression de clients", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

        Dim n As Integer
        n = DGClients.CurrentRow.Cells(0).Value()

        If result = DialogResult.Yes Then
            MySQLConnect.MaRequete = "DELETE FROM Client WHERE idClient = " & n & ""
            MySQLConnect.MaCommandeSpecialeRequete.CommandText = MySQLConnect.MaRequete
            MySQLConnect.MaCommandeSpecialeRequete.ExecuteNonQuery()

            MessageBox.Show("Client supprimé !", "Suppression de clients", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)

            position = 0
            RechargerLesDonnees()
            Rafraichir()
        Else
            Rafraichir()
            RechargerLesDonnees()
        End If
    End Sub

    Private Sub btnModifier_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModifier.Click
        'txtNum.Text = DGClients.CurrentRow.Cells(0).Value()
        'txtNom.Text = DGClients.CurrentRow.Cells(1).Value()
        'txtPrenom.Text = DGClients.CurrentRow.Cells(2).Value()
        'txtRue.Text = DGClients.CurrentRow.Cells(3).Value()
        'txtCodePostal.Text = DGClients.CurrentRow.Cells(4).Value()
        'txtVille.Text = DGClients.CurrentRow.Cells(5).Value()

            Dim num As Integer = CInt(txtNum.Text)
            Dim n = txtNom.Text
            Dim p As String = txtPrenom.Text
            Dim r As String = txtRue.Text
            Dim c As String = txtCodePostal.Text
            Dim v As String = txtVille.Text

            btnModifier.Text = "Valider modification !"

            MySQLConnect.MaRequete = "UPDATE Client Set NomClient = '" & n & "', PrenomClient = '" & p & "', AdRueClient = '" & r & "', AdCpClient = " & c & ", AdVilleClient = '" & v & "' where idClient = " & num & ""
            MySQLConnect.MaCommandeSpecialeRequete.CommandText = MySQLConnect.MaRequete
            MySQLConnect.MaCommandeSpecialeRequete.ExecuteNonQuery()

            MessageBox.Show("Client modifié !", "Modification de clients", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)

            Rafraichir()
            RechargerLesDonnees()
    End Sub

#End Region

#Region "Méthodes et procédures"

    Public Sub Rafraichir()
        txtNum.Text = MySQLConnect.MonJeuDeDonnees.Tables("TousLesClients").Rows(position).Item(0)
        txtNom.Text = MySQLConnect.MonJeuDeDonnees.Tables("TousLesClients").Rows(position).Item(1)
        txtPrenom.Text = MySQLConnect.MonJeuDeDonnees.Tables("TousLesClients").Rows(position).Item(2)
        txtRue.Text = MySQLConnect.MonJeuDeDonnees.Tables("TousLesClients").Rows(position).Item(3)
        txtCodePostal.Text = MySQLConnect.MonJeuDeDonnees.Tables("TousLesClients").Rows(position).Item(4)
        txtVille.Text = MySQLConnect.MonJeuDeDonnees.Tables("TousLesClients").Rows(position).Item(5)
    End Sub

    Public Sub RechargerLesDonnees()
        MySQLConnect.MonJeuDeDonnees.Tables("TousLesClients").Clear()
        MySQLConnect.MaRequete = "SELECT * FROM Client"
        MySQLConnect.MaCommandeSpecialeRequete.CommandText = MySQLConnect.MaRequete
        MySQLConnect.MonRemplisseur.SelectCommand = MySQLConnect.MaCommandeSpecialeRequete
        MySQLConnect.MonRemplisseur.Fill(MySQLConnect.MonJeuDeDonnees, "TousLesClients")
    End Sub
#End Region
End Class